// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Collections.Generic;

namespace MatrixFramework.Core.Results;

/// <summary>
/// 结果基类
/// </summary>
public abstract class ResultBase
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool IsSuccess { get; protected set; }

    /// <summary>
    /// 错误信息列表
    /// </summary>
    public IReadOnlyList<string> Errors { get; private set; } = new List<string>();

    /// <summary>
    /// 错误信息（单个）
    /// </summary>
    public string? Error => Errors.Count > 0 ? string.Join("; ", Errors) : null;

    /// <summary>
    /// 添加错误信息
    /// </summary>
    /// <param name="error">错误信息</param>
    protected void AddError(string error)
    {
        if (string.IsNullOrEmpty(error)) return;

        var errors = new List<string>(Errors);
        errors.Add(error);
        Errors = errors.AsReadOnly();
    }

    /// <summary>
    /// 添加多个错误信息
    /// </summary>
    /// <param name="errors">错误信息列表</param>
    protected void AddErrors(IEnumerable<string> errors)
    {
        if (errors == null) return;

        var errorList = new List<string>(Errors);
        errorList.AddRange(errors.Where(e => !string.IsNullOrEmpty(e)));
        Errors = errorList.AsReadOnly();
    }

    /// <summary>
    /// 设置错误信息
    /// </summary>
    /// <param name="errors">错误信息列表</param>
    protected void SetErrors(IReadOnlyList<string> errors)
    {
        Errors = errors ?? new List<string>().AsReadOnly();
    }
}